<?php

namespace app\index\controller;

use app\common\model\AppraiseModel;
use think\Controller;
use think\Db;
<<<<<<< HEAD
use app\common\model\TourOrderModel as to;
use think\Loader;
=======
use app\common\model\TourOrderModel as to; 
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad

class Tour extends Controller
{

    /**景点列表**/
    public function tourList()
    {
        $kinds = input('param.kinds');
        $page = input('param.page');
        $title = input('param.title');

        $where['is_del'] = 0;
        $where['is_display'] = 0;
        switch ($kinds) {
            case 1:
                $where['type'] = 1;
                break;
            case 2:
                $where['type'] = 2;
                break;
            case 3:
                $where['type'] = 3;
                break;
            case 4:
                $where['type'] = 4;
                break;
        }

        if($title){
            $where['tour_name'] = ['like','%'.$title.'%'];
        }

        if (!$page){
            $page = 1;
        }

        //查询景点列表
        $tour = Db::name('tours')
                ->field('tour_id,tour_name,tour_subject,tour_tag,sale_sum,tour_tag')
                ->where($where)
                ->order('sort desc')
                ->paginate(6,false,['query' => request()->param()]);


        $list = $tour->items();
        foreach ($list as $k => $v){

            //查询最低价格
            $ticket = Db::name('tour_ticket')
                ->field('price,discount_price,is_discount')
                ->where('is_del',0)
                ->where('is_use',1)
                ->where('tour_id',$v['tour_id'])
                ->select();

            if (empty($ticket)){
                $list[$k]['minprice'] = '暂无';
            }else{

                foreach ($ticket as $k2 => $v2){

                    if ($v2['is_discount']){

                        $minPrice[] = $v2['discount_price']*1;

                    }else{

                        $minPrice[] = $v2['price']*1;

                    }

                }

                if (count($minPrice)>1){
                    $list[$k]['minprice'] = min($minPrice);
                }else{
                    $list[$k]['minprice'] = $minPrice[0];
                }

            }

            //标签转数组
            $list[$k]['tour_tag'] = explode(',',$v['tour_tag']);

            //查询好评率
            $Appraise = new AppraiseModel();
            $list[$k]['rate'] = $Appraise->getAppRate(2,$v['tour_id']);

            //查询前三张图
            $list[$k]['img'] = Db::name('tour_img')->field('img_path')->where('tour_id',$v['tour_id'])->order('sort','desc')->limit(0,3)->select();

        }

        $this->assign('tour',$tour);
        $this->assign('list',$list);
        return view();
    }

    /**景点详情**/
    public function tour($id)
    {

        //查询景点信息
        $tour = Db::name('tours')->where('tour_id',$id)->find();
        $tour['tour_tag'] = explode(',',$tour['tour_tag']);

        $Appraise = new AppraiseModel();
        $tour['rate'] = $Appraise->getAppRate(2,$id);

        //统计评论条数
        $tour['comment_count'] = Db::name('appraise')->where('shop_id',$id)->where('is_display',0)->where('type',2)->count();
        $tour['latests_comment'] = Db::name('appraise')->field('content')->where('shop_id',$id)->where('type',2)->order('app_id','desc')->find();

        //查询前三张图片
        $tourImg = Db::name('tour_img')->field('img_path')->where('tour_id',$id)->order('sort','desc')->limit(0,3)->select();

        $imgCount = Db::name('tour_img')->field('img_path')->where('tour_id',$id)->count();

    

        //查询所有门票
        $ticketWhere['tour_id'] = $id;

        $ticket = Db::name('tour_ticket')->where('tour_id',$id)->order('sort','desc')->select();

        //判断是否数收藏该酒店
        $is_collect = 0;

        if (session('uid')) {
            $find_collect = Db::name('collect')->where('type',1)->where('other_id',$id)->find();

            if (!empty($find_collect)) {
                $is_collect = 1;
            }
        }
        
        $this->assign('is_select',$is_collect);
        $this->assign('tour',$tour);
        $this->assign('tourImg',$tourImg);
        $this->assign('imgCount',$imgCount);
        $this->assign('ticket',$ticket);

        return view();
    }

    /**获取景点评价**/
    public function getComment()
    {

        if (request()->isAjax()){

            $param = request()->param();
            $comment = Db::name('appraise')
                    ->alias('t1')
                    ->field('t1.content,t1.images,t1.eval_status,t1.create_time,t2.nickname,t2.user_icon')
                    ->join('lv_user t2','t1.user_id = t2.uid','left')
                    ->where('shop_id',$param['tour_id'])
                    ->where('type',2)
                    ->page($param['page'],5)
                    ->select();

            foreach ($comment as $k => $v){

                if ($v['images']){
                    $comment[$k]['images'] = explode(',',$v['images']);
                }else{
                    $comment[$k]['images'] = [];
                }

                $comment[$k]['create_time'] = date("Y-m-d H:i",$v['create_time']);

            }

            return ['code'=>0,'data'=>$comment];

        }
    }

    public function createOrder()
    {
        if (request()->isAjax()) {
            
            $to = new to();
            $result = $to->submitTourOrder(); 
<<<<<<< HEAD

        }else{

            $param = request()->param();
            //查询门票详情
            $ticket = Db::name('tour_ticket')
                    ->alias('t1')
                    ->field('t1.*,t2.tour_name,t2.is_del as tour_is_del,t2.is_display as tour_is_display,t2.is_pass as tour_is_pass')
                    ->join("lv_tours t2",'t1.tour_id = t2.tour_id','left')
                    ->where('t1.tid',$param['ticket_id'])
                    ->find();

            if ($ticket['tour_is_del'] == 1 || $ticket['tour_is_display'] == 1 || $ticket['tour_is_pass'] == 0 || $ticket['tour_is_pass'] == 2 || $ticket['is_use'] == 0 || $ticket['is_del'] == 1) {
                $this->error('该门票不可购买',url('Index/index'));
            }


            $this->assign('ticket',$ticket);
            $this->assign('param',$param);
            return view();

        }
    }

    public function orderPay()
    {
        $param = request()->param();

        //查询订单信息
        $orderInfo = Db::name('tour_order')->where('order_sn',$param['order_sn'])->find();

        $this->assign('orderInfo',$orderInfo);
        return view();
    }

    public function surePay(){

        if (request()->post()){
            $param = request()->param();

            //查询订单信息
            $order = Db::name('tour_order')
                    ->alias('t1')
                    ->field('t1.*,t2.tour_name')
                    ->join('lv_tours t2','t1.tour_id = t2.tour_id','left')
                    ->where('order_id',$param['order_id'])
                    ->find();

            if ($param['pay_type'] == 1){

                Loader::import('Alipay.aop.AopClient');
                $aop = new \AopClient();

                $config = config('alipay');
                $aop->gatewayUrl = $config['gatewayUrl'];
                $aop->appId = $config['appId'];
                $aop->rsaPrivateKey = $config['rsaPrivateKey'];
                $aop->format = $config['format'];
                $aop->postCharset = $config['postCharset'];
                $aop->signType = $config['signType'];
                $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];

                Loader::import('Alipay.aop.request.AlipayTradePagePayRequest');

                $request = new \AlipayTradePagePayRequest();

                $bizcontentArr= array(
                    'subject'=>$order['tour_name'].'门票预定-'.'沂水旅游',
                    'out_trade_no'=>$order['order_sn'],
                    'total_amount'=>$order['order_price'],
                    'product_code'=>'FAST_INSTANT_TRADE_PAY'
                );

                $request->setNotifyUrl('http://www.lancly.com/index/notify/alipayTour');
                $request->setReturnUrl('http://www.lancly.com/index/tour/payOver');

                $bizcontent = json_encode($bizcontentArr,true);

                $request->setBizContent($bizcontent);

                $response = $aop->pageExecute($request);

                echo $response;



            }else{

            }


        }
    }

    public function payOver()
    {
        $param = request()->param();

        $user_id = session('uid');
        $user = Db::name('user')->field('phone')->where('uid',$user_id)->find();
        $user['phone'] = substr($user['phone'],0,3).'****'.substr($user['phone'],-4);
        $this->assign('user',$user);
        $this->assign('param',$param);
=======

        }else{

            $param = request()->param();
            //查询门票详情
            $ticket = Db::name('tour_ticket')
                    ->alias('t1')
                    ->field('t1.*,t2.tour_name,t2.is_del as tour_is_del,t2.is_display as tour_is_display,t2.is_pass as tour_is_pass')
                    ->join("lv_tours t2",'t1.tour_id = t2.tour_id','left')
                    ->where('t1.tid',$param['ticket_id'])
                    ->find();

            if ($ticket['tour_is_del'] == 1 || $ticket['tour_is_display'] == 1 || $ticket['tour_is_pass'] == 0 || $ticket['tour_is_pass'] == 2 || $ticket['is_use'] == 0 || $ticket['is_del'] == 1) {
                $this->error('该门票不可购买',url('Index/index'));
            }


            $this->assign('ticket',$ticket);
            $this->assign('param',$param);
            return view();

        }
    }

    public function orderPay()
    {
        $param = request()->param();

        //查询订单信息
        $orderInfo = Db::name('tour_order')->where('order_sn',$param['order_sn'])->find();

        $this->assign('orderInfo',$orderInfo);
        return view();
    }

    public function payOver()
    {
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad
        return view();
    }

}
